<?php
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = $_SESSION['user'];

$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);

if($row['type'] != 2)
	header("Location: home.php");
?>
<!doctype html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, maximum-scale=1, initial-scale=1, user-scalable=0">
    <link rel="icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
    <link rel="shortcut icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
  <!-- Always force latest IE rendering engine or request Chrome Frame -->
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">

  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,800">

  <!-- Use title if it's in the page YAML frontmatter -->
  <title>ELO Edge Order Tracking</title>

  <!--[if lt IE 9]>
  <script src="../../javascripts/vendor/html5shiv.js" type="text/javascript"></script>
  <script src="../../javascripts/vendor/excanvas.js" type="text/javascript"></script>
  <![endif]-->
<link rel="stylesheet" href="css/bootstrap.min.css" />
		<link rel="stylesheet" href="css/font-awesome.css" />
		<link rel="stylesheet" href="css/fullcalendar.css" />
		<link rel="stylesheet" href="css/jquery.jscrollpane.css" />	
		<link rel="stylesheet" href="css/unicorn.css" />
		
  <link href="stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
  <script src="javascripts/application.js" type="text/javascript"></script>
   <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
	<script src="//code.jquery.com/jquery-1.9.1.js"></script>
	<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
	 <style>
		#message_sort { list-style-type: none; margin: 0; padding: 0; width: 50%;}
		#message_sort li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
		#message_sort li span { position: absolute; margin-left: -1.3em; }
		
		</style>
		<script>
		$(function() {
		$( "#message_sort" ).sortable({
		start: function(event, ui) {
            $(ui.helper).addClass("sortable-drag-clone");
        },
        stop: function(event, ui) {
            $(ui.helper).removeClass("sortable-drag-clone");
        },
		update: function(event, ui) {
			change_message_order();
		}
		});
		$( "#message_sort" ).disableSelection();
		});
		
	</script>
</head>

<?php
	include("top.php");
?>

  <div class="container-fluid padded">
    <div class="row-fluid">

      <!-- Breadcrumb line -->

      <div id="breadcrumb">
	  <a href="#" title="Go to Home" class="tip-bottom"><i class="fa fa-home"></i> Home</a>
					<a href="#" >Ticket</a>
					<a href="#" class="current"> Email Notifications  </a>
              
            </div>
    </div>
  </div>

  <div class="container-fluid padded">
  <div class="row-fluid">
   <div class="col-lg-12">
      <div class="box">
         <div class="box-header">
            <span class="title">New Message</span>
         </div>
         <div class="box-content">
            <table class="table table-normal">
               <thead>
                  <tr>
                     <td>Name</td>
                     <td>Value</td>
					 <td>Action</td>
                  </tr>
               </thead>
               <tbody>
			   <form action = 'add_email_notification.php' method = 'post'>
			   <tr><td><input type = 'text' name = 'name' /></td>
				<td><textarea name = 'message' style = 'width: 700px; height: 300px;'></textarea></td>
				<td><input type = 'submit' class='btn btn-success btn-xs' value = 'Create' /></td></tr></form>
			   </tbody>
            </table>
         </div>
      </div>
   </div>
</div>
<div class="row-fluid">
   <div class="col-lg-12">
      <div class="box">
         <div class="box-header">
            <span class="title">Reorder Messages</span>
         </div>
         <div class="box-content">
            <ul id = "message_sort">
			<?php
				$result = mysql_query("SELECT * FROM email_notification ORDER BY defined_order") or die(mysql_error());

				while($row = mysql_fetch_array($result))
				{
					echo '<li id = "'.$row['id'].'" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>'.$row['name'].'</li>';
				}
			?>
			</ul>
         </div>
      </div>
   </div>
</div>
  <div class="row-fluid">
   <div class="col-lg-12">
      <div class="box">
         <div class="box-header">
            <span class="title">Edit Messages</span>
         </div>
         <div class="box-content">
            <table class="table table-normal">
               <thead>
                  <tr>
                     <td>Name</td>
                     <td>Value</td>
					 <td>Action</td>
                  </tr>
               </thead>
               <tbody>
			   <?php
					$result = mysql_query("SELECT * FROM email_notification") or die(mysql_error());

					while($row = mysql_fetch_array($result))
					{
						echo "<form action = 'edit_email_notification.php' method = 'post'>";
						echo "<input name = 'id' value = '".$row['id']."' type = 'hidden' />";
						echo "<tr><td><input type = 'text' name = 'name' value = '".$row['name']."'/></td>";
						echo "<td><textarea name = 'message' style = 'width: 700px; height: 300px;'>".stripslashes($row['message'])."</textarea></td>";
						echo "<td><input type = 'submit' class='btn btn-success btn-xs' value = 'Update' /></td></tr></form>";
					}
			   ?>
			   </tbody>
            </table>
         </div>
      </div>
   </div>
</div>
</div>
<script>
function change_message_order ()
{
	var x = 0;
	var list = '';
	var booster_sort = document.getElementById("message_sort");

	while(booster_sort.children[x] != null)
	{
		list += booster_sort.children[x].id+",";
		x++;
	}

	list = list.slice(0, list.length-1);

	var ajaxHandler = new XMLHttpRequest();

	var form = new FormData();

	form.append("order", list);

	ajaxHandler.open("POST", "change_email_order.php");
	ajaxHandler.send(form);
}
</script>
</div>
<?php
	include("bottom.php");
?>
